/*
Copyright (c) 2006 Stanisław Pitucha

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

using System;
using Gtk;
using System.Text.RegularExpressions;

namespace GtkRegexTest {
	public class RegexTreeSelectionArgs : EventArgs {
		int[] _match, _group, _capture;
		
		public RegexTreeSelectionArgs (int[] match, int[] group, int[] capture) {
			_match=match;
			_group=group;
			_capture=capture;
		}
		
		public int[] Match {
			get { return _match; }
		}

		public int[] Group {
			get { return _group; }
		}
		
		public int[] Capture {
			get { return _capture; }
		}
	}
	
	public class RegexTree : Gtk.Bin {
		protected Gtk.TreeView internalTree;
		
		public delegate void RegexTreeSelectionChangeHandler(object tv, RegexTreeSelectionArgs e);
		public event RegexTreeSelectionChangeHandler RegexTreeSelectionChange;
		
		public RegexTree() {
			Stetic.Gui.Build(this, typeof(GtkRegexTest.RegexTree));
			internalTree.Model = new TreeStore(typeof(string), typeof(int), typeof(int));
			internalTree.AppendColumn("Match", new CellRendererText(), "text", 0);
			internalTree.AppendColumn("Start", new CellRendererText(), "text", 1);
			internalTree.AppendColumn("Length", new CellRendererText(), "text", 2);
		}
		
		public void Clear() {
			(internalTree.Model as TreeStore).Clear();
		}
		
		public void Add(MatchCollection mc) {
			foreach(Match m in mc) {
				Add(m);
			}
		}
		
		public void Add(Match m) {
			TreeStore model = (TreeStore)internalTree.Model;

			TreeIter root = model.AppendValues(m.ToString(), m.Index, m.Length);
			foreach(Group g in m.Groups) {
				TreeIter gIter = model.AppendValues(root, g.ToString(), g.Index, g.Length);
				foreach(Capture c in g.Captures) {
					model.AppendValues(gIter, c.ToString(), c.Index, c.Length);
				}
			}
		}
		
		public void OnRegexTreeSelectionChange(object tv, RegexTreeSelectionArgs e) {
			if(RegexTreeSelectionChange!=null)
				RegexTreeSelectionChange(tv, e);
		}

		protected int[] getValues(TreeView tv, TreePath tp) {
			TreeIter ti;
			tv.Model.GetIter(out ti, tp);
			int s = (int)tv.Model.GetValue(ti, 1);
			int l = (int)tv.Model.GetValue(ti, 2);
			
			return new int[2] {s, l};
		}

		protected virtual void internalTreeCursorChanged(object sender, System.EventArgs e) {
			TreeView tv = (TreeView)sender;
			TreePath tp;
			TreeViewColumn tc;
			tv.GetCursor(out tp, out tc);
			int[] selected = getValues(tv, tp);
			
			int[] capture=null;
			int[] group=null;
			int[] match;
			if(tp.Depth>1) {
				if(tp.Depth>2) {
					capture=selected;
					tp.Up();
					selected = getValues(tv, tp);
				}
				group=selected;
				tp.Up();
				selected = getValues(tv, tp);
			}
			match = selected;

			OnRegexTreeSelectionChange(this, new RegexTreeSelectionArgs(match, group, capture));
		}
	}
}
